package example.service.impl;

import example.common.FileUtils;
import example.common.Result;
import example.entity.Secondment;
import example.entity.SelectionTemplate;
import example.entity.Template;
import example.mapper.SelectionTemplateMapper;
import example.mapper.TemplateMapper;
import example.service.ITemplateService;
import example.vo.TemplateVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class TemplateServiceImpl implements ITemplateService {

    @Value("${file.template_dir}")
    private String templateDir;

    @Resource
    private TemplateMapper templateMapper;

    @Resource
    private SelectionTemplateMapper selectionTemplateMapper;

    @Override
    public Result getTemplateList() {
        List<TemplateVO> templateList = templateMapper.getTemplateList();
        return Result.ok(templateList);
    }

    @Override
    public Result addTemplate(Template template) {
        String name = template.getName();
        templateMapper.save(name);
        return Result.ok("添加成功！");
    }

    @Override
    public Result uploadTemplate(MultipartFile file, Long templateId) {
        String fileName = StringUtils.cleanPath(Objects.requireNonNull(file.getOriginalFilename()));
        String filePath = templateDir + "\\" + fileName;
        File newFile = new File(filePath);
        newFile.getParentFile().mkdirs();
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            return Result.fail("上传失败！");
        }
        Template template = new Template();
        template.setId(templateId);
        template.setFileName(fileName);
        template.setFilePath(filePath);
        template.setDisabled(true);
        templateMapper.saveTemplate(template);
        return Result.ok("上传成功！");
    }

    @Override
    public void downTemplate(Long id, HttpServletResponse response) {
        Template template = templateMapper.getById(id);
        String pdfName = template.getFileName();
        String pdfPath = template.getFilePath();
        FileUtils.download(pdfName, pdfPath, response);
    }

    @Override
    public Result getFileName(Long id) {
        String name = templateMapper.getName(id);
        return Result.ok(name);
    }

    @Override
    public Result delTemplate(Long id) {
        templateMapper.delTemplate(id);
        return Result.ok("删除成功！");
    }

    @Override
    public Result getTemplateNames() {
        List<String> names = templateMapper.getTemplateNames();
        return Result.ok(names);
    }

    @Override
    public Result addSelectionTemplate(Long selectionId, List<String> templateNames) {
        try {
            for (String name : templateNames) {
                Long templateId = templateMapper.getIdByName(name);
                SelectionTemplate selectionTemplate = new SelectionTemplate();
                selectionTemplate.setSelectionId(selectionId);
                selectionTemplate.setTemplateId(templateId);
                selectionTemplateMapper.save(selectionTemplate);
            }
            return Result.ok("选择模版成功！");
        } catch (Exception e) {
            return Result.fail("请不要添加重复的模版！");
        }
    }

    @Override
    public Result getL(Long selectionId) {
        List<Long> tId = selectionTemplateMapper.getIds(selectionId);
        List<TemplateVO> templateVOList = new ArrayList<>();
        for (Long ttId : tId) {
            TemplateVO templateVO =  templateMapper.getInfo(ttId);
            if (templateVO != null) {
                templateVO.setId(ttId);
                templateVOList.add(templateVO);
            }

        }
        return Result.ok(templateVOList);
    }

    @Override
    public Result delTemp(Long id) {
        selectionTemplateMapper.delTemp(id);
        return Result.ok("删除成功！");
    }
}
